﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace ATTAR
{
    public static class ValidationConfigurationSerialization
    {
        public static void ValidationConfiguration_Serialize(ValidationConfiguration configuration)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(configuration.GetType());

            System.Xml.XmlWriter file;

            using (file = System.Xml.XmlWriter.Create(@"ValidationConfiguration.xml"))
            {
                x.Serialize(file, configuration);
            }            
        }

        public static void SaveWorkingDll(string workingDllPath)
        {            
            string workingDll = workingDllPath;

            XmlDocument doc = new XmlDocument();                       
            doc.Load(@"ValidationConfiguration.xml");
            XmlNodeList nodes = doc.GetElementsByTagName("workingDllPath");
            XmlNode workingDllNode = nodes[0];
            if (workingDllNode == null)
            {
                workingDllNode = doc.CreateNode("element", "workingDllNode", "ATTAR");
            }

            workingDllNode.InnerText = string.IsNullOrEmpty(workingDllPath) ? "" : workingDllPath;

            doc.Save(@"ValidationConfiguration.xml");
        }

        public static ValidationConfiguration ValidationConfiguration_Deserialize(string fileName)
        {
            ValidationConfiguration config = null;
            string path = fileName;
            XmlSerializer serializer = new XmlSerializer(typeof(ValidationConfiguration));

            using (StreamReader reader = new StreamReader(path))
            {                
                config = (ValidationConfiguration)serializer.Deserialize(reader);                
            }

            return config;
        }
    }
}
